import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    redirect: '/homePage',
    children: [
      {
        path: '/homePage',
        name: 'homePage',
        component: () => import('../components/homePage.vue')
      },
      {
        path: '/homeInspiration',
        name: 'homeInspiration',
        component: () => import('../components/homeInspiration.vue')
      },
      {
        path: '/studyroom',
        name: 'studyroom',
        component: () => import('../components/studyroom.vue')
      },
      {
        path: '/livingroom',
        name: 'livingroom',
        component: () => import('../components/livingroom.vue')
      },
      {
        path: '/kitchen',
        name: 'kitchen',
        component: () => import('../components/kitchen.vue')
      },
      {
        path: '/diningroom',
        name: 'diningroom',
        component: () => import('../components/diningroom.vue')
      },
      {
        path: '/bathroom',
        name: 'bathroom',
        component: () => import('../components/bathroom.vue')
      },
      {
        path: '/hallroom',
        name: 'hallroom',
        component: () => import('../components/hallroom.vue')
      },
      {
        path: '/aboutUs',
        name: 'aboutUs',
        component: () => import('../components/aboutUs.vue')
      },
      {
        path: '/mine',
        name: 'mine',
        component: () => import('../components/mine.vue')
      },
      {
        path: '/itemDetails',
        name: 'itemDetails',
        component: () => import('../components/itemDetails.vue')
      },
      {
        path: '/productStyle',
        name: 'productStyle',
        component: () => import('../components/productStyle.vue')
      },
      {
        path: '/nordicstyle',
        name: 'nordicstyle',
        component: () => import('../components/nordicstyle.vue')
      },
      {
        path: '/simplestyle',
        name: 'simplestyle',
        component: () => import('../components/simplestyle.vue')
      },
      {
        path: '/neochinese',
        name: 'neochinese',
        component: () => import('../components/neochinese.vue')
      },
      {
        path: '/lightluxury',
        name: 'lightluxury',
        component: () => import('../components/lightluxury.vue')
      },
      {
        path: '/wabiSabi',
        name: 'wabiSabi',
        component: () => import('../components/wabiSabi.vue')
      },
      {
        path: '/productDetails',
        name: 'productDetails',
        component: () => import('../components/productDetails.vue')
      },
      {
        path: '/collect',
        name: 'collect',
        component: () => import('../components/collect.vue')
      },
      {
        path: '/designScheme',
        name: 'designScheme',
        component: () => import('../components/designScheme.vue')
      }
    ]
  },
  {
    path: '/',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,

  // 设置滚动行为
  scrollBehavior(to, from, savedPosition) {
    // 如果有保存的位置，则滚动到该位置（用于浏览器的前进和后退按钮）
    if (savedPosition) {
      return savedPosition;
    } else {
      // 否则滚动到页面顶部
      return { x: 0, y: 0 };
    }
  },
})

// 添加路由守卫
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!isLoggedIn()) {
      next('/login');
    } else {
      next();
    }
  } else {
    next();
  }
});

// 检查用户是否已登录
function isLoggedIn() {
  let login = Cookies.get('token')
  if (login != undefined) {
    return true;
  } else {
    return false;
  }
}

export default router
